/* app02-window.c
 *
 * Copyright 2024 wan
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "app02-config.h"
#include "app02-window.h"
#include "./data/user.h"
#include "./main/main-window.h"
struct _App02Window
{
  GtkApplicationWindow  parent_instance;

  /* Template widgets */
  GtkHeaderBar        *header_bar;
  //1.声明组件
  // 注意：类型和变量名一定要与ui中的组件属性一致
  GtkButton *btn_login;
  GtkEntry *name_input;//账户
  GtkEntry *pwd_input;//密码
  
};

G_DEFINE_TYPE (App02Window, app02_window, GTK_TYPE_APPLICATION_WINDOW)

static void
app02_window_class_init (App02WindowClass *klass)
{
  GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);

  gtk_widget_class_set_template_from_resource (widget_class, "/a/b/c/app02-window.ui");
  
  gtk_widget_class_bind_template_child (widget_class, App02Window, header_bar);
  //2.将组件设置到窗体上
  gtk_widget_class_bind_template_child (widget_class, App02Window, btn_login);
  gtk_widget_class_bind_template_child (widget_class, App02Window, name_input);
  gtk_widget_class_bind_template_child (widget_class, App02Window, pwd_input);
}
GtkWidget* login_window; //解决主创体隐藏，显示登录登录窗体的问题，设置成员变量即可
GtkEntry *name;
GtkEntry *password;

gboolean main_window_hide(GtkWidget *window){
  //隐藏主窗体
    gtk_widget_hide(window);
    //GdkWindow *login_window =  gtk_widget_get_window (GTK_WIDGET (data));
    //g_object_get(data,NULL);
    //显示登录
    gtk_widget_show(login_window);
  
  return TRUE;
}
//处理登录的函数
void handle_login(GtkButton*btn,gpointer user_data){
  //获取账户
  char * login_name = (char*)gtk_entry_get_text (name); 
  printf("账户:%s\n",login_name);
  // 获取密码
  char *login_pwd = (char*)gtk_entry_get_text (password);
  printf("密码:%s\n",login_pwd);
  //数据库进行验证
  int res = get_user_name (login_name,login_pwd);
  if(res){
    //登录失败
  }else{
    //登录成功
    printf("登录成功...\n");
    GtkWindow *main_window;
    main_window = g_object_new(MAIN_TYPE_WINDOW,NULL);
    //英藏登录窗体
    gtk_widget_hide (GTK_WIDGET (user_data));
    gtk_widget_show_all (GTK_WIDGET (main_window));
    g_signal_connect (main_window, "delete_event", G_CALLBACK(main_window_hide),NULL);
    
  }
}
static void
app02_window_init (App02Window *self)
{
  gtk_widget_init_template (GTK_WIDGET (self));
  login_window =GTK_WIDGET (self); 
  //给成员变量设置值
  name = self->name_input;
  password =self->pwd_input;
  //3.使用这个组件
  // 为这个登录按钮设置单击信号处理
  // 产生信号的组件；信号量，回调函数，传递的值
  g_signal_connect(self->btn_login,"clicked",G_CALLBACK(handle_login),self);
  
}
